package base;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.peng.auctionsystem.R;

import mvp.BaseModel;
import mvp.BasePresenter;
import mvp.RxManager;
import utils.ToastUtil;


/**
 * Created by Mr.Q on 2018/3/10.
 * 描述
 */

public abstract class BaseFragment<T extends BasePresenter, E extends BaseModel> extends Fragment {
    protected View rootView;
    public T mPresenter;
    public E mModel;
    public RxManager mRxManager;
    public BaseActivity mActivity;
    private Dialog mDialog;


    @Override
    public void onAttach(Context context) {
        mActivity = (BaseActivity) context;
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(getLayoutId(), container, false);
        }
        //管理RxBus和RxJava的生命周期
        mRxManager = new RxManager();
//        mPresenter = TUtil.getT(this, 0);
//        mModel= TUtil.getT(this,1);
        this.initPresenter();
        //如果使用MVP且Presenter初始化成功
        if (mPresenter != null) {
            mPresenter.mContext = this.getActivity();
        }

        return rootView;
    }

    //获取布局文件
    protected abstract int getLayoutId();

    /**
     * 简单页面无需mvp就不用管此方法即可,完美兼容各种实际场景的变通
     */
    protected void initPresenter() {

    }


    protected void goneView(@NonNull View... views) {
        for (int i = 0; i < views.length; i++) {
            views[i].setVisibility(View.GONE);
        }
    }

    protected void invisibleView(@NonNull View... views) {
        for (int i = 0; i < views.length; i++) {
            views[i].setVisibility(View.INVISIBLE);
        }
    }

    protected void visibleView(@NonNull View... views) {
        for (int i = 0; i < views.length; i++) {
            views[i].setVisibility(View.VISIBLE);
        }
    }


    /**
     * 通过Class跳转界面
     **/
    public void startActivity(Class<?> cls) {
        startActivity(cls, null);
    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, int requestCode) {
        startActivityForResult(cls, null, requestCode);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
        getActivity().overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }


    

    /**
     * 停止加载进度条
     */
    public void stopProgressDialog() {
        if (mDialog != null) {
            mDialog.cancel();
        }
    }


    /**
     * 短暂显示Toast提示(来自String)
     **/
    public void showShortToast(String text) {
        ToastUtil.showShort(text);
    }

    /**
     * 短暂显示Toast提示(id)
     **/
    public void showShortToast(int resId) {
        ToastUtil.showShort(resId);
    }

    /**
     * 长时间显示Toast提示(来自res)
     **/
    public void showLongToast(int resId) {
        ToastUtil.showLong(resId);
    }

    /**
     * 长时间显示Toast提示(来自String)
     **/
    public void showLongToast(String text) {
        ToastUtil.showLong(text);
    }


    public void showToastWithImg(String text, int res) {
        ToastUtil.showToastWithImg(text, res);
    }

    /**
     * 网络访问错误提醒
     */
//    public void showNetErrorTip() {
//        ToastUtil.showToastWithImg(getText(R.string.net_error).toString(), R.drawable.msg_icon_fail);
//    }
//
//    public void showNoNetTip() {
//        ToastUtil.showToastWithImg(getText(R.string.no_net).toString(), R.drawable.msg_icon_fail);
//    }
//
//    public void showNetErrorTip(String error) {
//        ToastUtil.showToastWithImg(error, R.drawable.msg_icon_fail);
//    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        rootView = null;
        if (mPresenter != null) {
            mPresenter.onDestroy();
        }
        mRxManager.clear();
    }
}

